<template>
  <!-- 基于 Element UI 新增和修改弹窗 -->
    <a-drawer
            :title="!dataForm.imgid ? '添加' : '修改'"
            :visible="visible"
            @close="cancel"
            :body-style="{ paddingBottom: '80px' }"
            :width="750"
    >
    <!-- 新增和创建表单表单 -->
    <el-form :model="dataForm" :rules="dataRule"  v-loading="loading" ref="dataForm" @keyup.enter.native="dataSubmit()" label-width="80px" style="padding: 0px 10px;margin: 0px 10px">
    <el-form-item label="排行榜" prop="oid">
        <el-select v-model="dataForm.oid" placeholder="请选择排行榜" class="selWidth" filterable>
          <el-option
            v-for="item in rankingsDataList"
            :label="item.name"
            :key="item.id"
            :value="item.id">
          </el-option>
        </el-select>
    </el-form-item>
    <el-form-item label="选择应用" prop="sid" v-if="!this.dataForm.id">
      <el-select
        v-model="dataForm.sid"
        filterable
        remote
        placeholder="请输入应用标题"
        :remote-method="querySoftwareList"
        :loading="softwareListloading">
        <el-option
          v-for="item in softwareList"
          :key="item.softwareid"
          :label="item.softwarename+' - '+(item.softwaretype==1?'Android':item.softwaretype==2?'IOS':'H5')"
          :value="item.softwareid">
        </el-option>
      </el-select>

    </el-form-item>
<!--    <el-form-item label="adminid" >-->
<!--      <el-input v-model="dataForm.adminid" placeholder="adminid" size="small"></el-input>-->
<!--    </el-form-item>-->
<!--    <el-form-item label="类型：0，排行榜关联" >-->
<!--      <el-input v-model="dataForm.type" placeholder="类型：0，排行榜关联" size="small"></el-input>-->
<!--    </el-form-item>-->
<!--    <el-form-item label="状态：0:未启用，1:启用，2、下架" >-->
<!--      <el-input v-model="dataForm.status" placeholder="状态：0:未启用，1:启用，2、下架" size="small"></el-input>-->
<!--    </el-form-item>-->
    <el-form-item label="排序" >
      <el-input type="number" v-model="dataForm.sortnum" placeholder="排序" size="small"></el-input>
    </el-form-item>
<!--    <el-form-item label="扩展" >-->
<!--      <el-input v-model="dataForm.extra" placeholder="扩展" size="small"></el-input>-->
<!--    </el-form-item>-->
    </el-form>
        <div :style="{position: 'absolute',right: 0,bottom: 0,width: '100%', borderTop: '1px solid #e9e9e9',padding: '10px 16px',background: '#fff',textAlign: 'right',zIndex: 1,}">
            <el-button @click="cancel()" :style="{ marginRight: '8px' }">取消</el-button>
            <el-button type="primary" @click="submit()">确定</el-button>
        </div>
  </a-drawer>
</template>

<script>
    import api from '@/api/api'
    export default {
    components: {
    },
    data () {
      return {
        visible: false,
        loading:false,
        softwareListloading:false,
        softwareList:[],
        rankingsDataList:[],
        dataForm: {
          id: 0,
          oid: 0 ,
          sid: '' ,
          adminid: '' ,
          type: '' ,
          status: '' ,
          sortnum: '' ,
          extra: '' ,
          createtime: '' ,
          updatetime: ''
        },
        dataRule: {
          oid: [
            { required: true, message: '排行榜为必选项', trigger: 'blur' }
          ],
          sid: [
            { required: true, message: '应用为必选项', trigger: 'blur' }
          ],
          adminid: [
            { required: true, message: 'adminid  为必填项', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '类型：0，排行榜关联  为必填项', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态：0:未启用，1:启用，2、下架  为必填项', trigger: 'blur' }
          ],
          sortnum: [
            { required: true, message: '排序  为必填项', trigger: 'blur' }
          ],
          extra: [
            { required: true, message: '扩展  为必填项', trigger: 'blur' }
          ],
          createtime: [
            { required: true, message: '创建日期  为必填项', trigger: 'blur' }
          ],
          updatetime: [
            { required: true, message: '更新日期  为必填项', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id,rankingsDataList,oid) { // 初始化表单验证规则
        this.dataForm.id = id || undefined
        this.rankingsDataList=rankingsDataList;
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id){
              this.loading = true
                api.post('rest/vabc/appcorrelation/info',{id:this.dataForm.id}).then(res => {
                this.dataForm = res;
            }).finally(() => {
                this.loading = false
            });
          }else {
            this.dataForm.oid=oid
          }
        })
      },
      // 表单数据提交
      submit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            let postData=Object.assign({},this.dataForm)
            postData.createtime=undefined
            postData.updatetime=undefined
            if (!this.dataForm.id){
                postData.id==undefined
                api.post('rest/vabc/appcorrelation/save',postData).then(res =>{
                    this.$message.success("创建成功")
                    this.reset()
                    this.visible = false
                    this.$parent.getDataList()
                }).finally(() => {
                    this.loading = false
                });
            }else{
                api.post('rest/vabc/appcorrelation/update',postData).then(res =>{
                    this.$message.success("保存成功")
                    this.reset()
                    this.visible = false
                    this.$parent.getDataList()
                }).finally(() => {
                    this.loading = false
                });
            }
          }
        })
      },
     cancel(){
        this.visible = false
        this.reset()
    },
    reset(){
        this.$refs.dataForm.resetFields()
        this.$data.dataForm=JSON.parse(JSON.stringify(this.$options.data().dataForm))
        this.softwareList=[]
    },
      querySoftwareList(query){
        if (query !== '') {
          this.softwareListloading = true;
          let postData={
            'order':'desc',
            'column':'softwareid'
          }
          postData.softwarename=api.getLikeValue(query)
          api.post('rest/vabc/softwareEntity/list',postData).then(res => {
            this.softwareList=res.datalist
          }).finally(() => {
            this.softwareListloading = false
          });
        } else {
          this.softwareList = [];
        }
      }
    }
  }
</script>
